package reese.think_in_java.java_21_thread;

/**
 * ***********************************************************
 *
 * @类名 : reese.think_in_java.java_21_thread.类名
 * @DESCRIPTION : 这家伙太懒啥都没写
 * @AUTHOR :  一个风情万种的男子
 * @DATE : 2016/5/29
 * ***********************************************************
 */

//using a named inner class
class InnerThread1 {
    private int countDown = 5;
    private Inner inner ;

    public InnerThread1(String name) {
        inner = new Inner(name);
    }

    private class Inner extends Thread{
        Inner(String name){
            super(name);
            start();
        }
        public void run(){
            while (true){
                try {
                    System.out.println(this);
                    if(--countDown==0){
                        sleep(10);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        public String toString(){
            return getName() + ":" +countDown ;
        }
    }


}

//using an anonymous inner class :

class InnerThread2 {
    private int countDown = 5;
    private Thread t ;
    public InnerThread2(String name) {
        t = new Thread(name){
            public void run() {
                while (true){
                    try {
                        System.out.println(this);
                        if(--countDown==0){
                            sleep(10);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
            public String toString(){
                return getName() + ":" +countDown ;
            }
        };
        t.start();
    }
}


public class ThreadVariations {

    public static void main(String[] args) {
        new InnerThread1("innerThread1");
        new InnerThread2("innerThread2");
    }



}
